{% extends 'layout/basic.html' %}
{% load static %}

{% block title %}
    index
{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
{% endblock %}

{% block content %}
    <div class="account">
        {#    <h2 class="text-center">注册页面</h2>#}
        <h2 class="title">注册页面</h2>
        <form id='regForm' method="post" novalidate>
            {% csrf_token %}

            {% for field in form %}


                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label> :
                        <div class='clearfix'>
                            <div class="col-xs-7" style="padding-left: 0"> {{ field }}
                                <span class="error-msg">{{ field.errors.0 }}</span>
                            </div>
                            <div class="col-xs-5"><input id='btnSms' type="button" class="btn btn-default"
                                                         value="点击获取验证码"></div>

                        </div>

                    </div>

                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label> :
                        {{ field }}
                        <span class="error-msg">{{ field.errors.0 }}</span>

                    </div>
                {% endif %}

            {% endfor %}
            <div class="row">
                <div class="col-xs-3">
                    <input id='btnSubmit' type="button" class="btn btn-primary" value="注 册">

                </div>

            </div>
        </form>
    </div>
{% endblock %}


{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            bindClickBtnSubmit();
        });
        // verify_code button click function
        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                {#alert(      $('#id_mobile_phone').val()#}
                {#alert($('#id_mobile_phone').val());#}
                $('.error-msg').empty();//remove last time's error info in span
                var mobilePhone = $('#id_mobile_phone').val();

                //   	发送Ajax请求
                $.ajax({
                    url: "/send/sms/",   //http://www.xxx.com/index/?k1=123&k2=567
                    type: 'GET',
                    {#data:{k1:123,k2:567},#}
                    data: {mobile_phone: mobilePhone, tpl: 'register'},
                    dataType: 'JSON',
                    success: function (res) {
                        {#res_direct = JSON.parse(res)#} //将后端的HttpResponse 的字符串 反序列化为字典 因为这里后端直接用得时JSonResponse
                        //顾不需要

                        if (res.status) {
                            console.log('发送成功 倒计时')

                            sendSmsRemind();
                        } else {
                            console.log(res);//{error:{mobile_phone:["这个字段是必填项。"],}status:false}
                            console.log('发送失败 错误信息')
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0]);

                            })
                        }

                    }

                })
            });
        }

        //the register button click function
        function bindClickBtnSubmit() {
            $('#btnSubmit').click(function () {

                $('.error-msg').empty();//remove last time's error info in span

                //   	发送Ajax请求
                $.ajax({
                    {#url: "/send/sms/",   //http://www.xxx.com/index/?k1=123&k2=567#}
                    url: "{% url 'register' %}",   //http://www.xxx.com/index/?k1=123&k2=567
                    type: 'POST',
                    
                    data: $('#regForm').serialize(),//// 所有字段数据 + csrf token
                    dataType: 'JSON',
                    success: function (res) {
                       if (res.status){
                           location.href = res.data;
                       }else {
                           $.each(res.error,function(key,value){
                               $('#id_'+key).next().text(value[0]);
                           })
                       }


                    }

                })
            });
        }

        // timer remind when click the verify_code button
        function sendSmsRemind() {

            var $smsBtn = $('#btnSms');
            $smsBtn.prop('disabled', true);

            var time = 60;
            var remind = setInterval(function () {
                $smsBtn.val(time+'秒重新发送');
                    time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                     $smsBtn.val('点击获取验证码').prop('disabled',false);

                }
            }, 1000)
        }

    </script>

{% endblock %}